<?php
	// http://simplehtmldom.sourceforge.net/manual.htm#section_access
	header('Content-Type: text/html; charset=utf-8'); 
	error_reporting(~0); ini_set('display_errors', 1);
	ini_set('memory_limit', '-1');
	chdir(dirname(__FILE__));
	include('simple_html_dom.php');
	include('../services/SignService.php');

	echo "start<br><br><br><br>";
	
	importTweets("alexa_chung","alexa");
	importTweets("TODAYshow","todayshow");
	importTweets("angelcandice","angel");
	importTweets("SofiaVergara","sofia");
	importTweets("emmyrossum","emmy");
	importTweets("EllenPage","ellen");
	importTweets("RealTalk","realtalk");
	importTweets("haydenpanettier","hayden");
	importTweets("veryinutil","veryinutil");
	importTweets("BreakingNews","BreakingNews");
	importTweets("MTV","MTV");
	importTweets("celebritygossip","celebritygossip");
	
	
	echo "<br><br><br><br>fine";
	
	
	
	
	function importTweets($twitterAccount,$nameMasker) {
		$url = 'https://twitter.com/search?q=from%3A'.$twitterAccount.'&src=typd&f=realtime';
		$fileName = "tweetIds.txt";
		
		
		$file = fopen($fileName, "r+") or exit("Unable to open file!");
		while(!feof($file)) {
			$tweetIds = fgets($file);
		}
		fclose($file);
		
		$html = file_get_html($url);
		$count = 0;
		foreach($html->find('.js-stream-item') as $tweet) {
				$tweetId = $tweet->children(0)->getAttribute("data-tweet-id");
				if(strlen(strstr($tweetIds,$tweetId)) == 0) {
					$tweetText = $tweet->find(".tweet-text",0)->plaintext;
					$data = $tweet->find(".js-stream-tweet",0)->getAttribute("data-expanded-footer");
					$tweetText = str_replace('http:// ', 'http://', $tweetText);
					$tweetText = str_replace('pic.twitter.com', 'http://pic.twitter.com', $tweetText);		

					preg_match_all('/\bhttps?:\/\/\S+(?:png|jpg|gif)\b/', $data, $images);
					$lastImage = "";
					foreach($images[0] as $image) {	
						if($lastImage != $image) {
							$lastImage = $image;
						}
					}
					$tweetText .= " ".$lastImage;

					//echo "<br><br><br><br>".$tweetText;
	
					
					insertJobSign($nameMasker ,$tweetText);
					file_put_contents($fileName, $tweetId."|", FILE_APPEND); 
					$count++;
				}
		}
		echo "<br>-----------> account ".$nameMasker." imported ".$count. " tweets";
	}
	
	
	
	
	

?>